<!doctype html>



  


<html class="theme-next mist use-motion" lang="zh-Hans">
<head>
  <meta charset="UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"/>






<meta http-equiv="Cache-Control" content="no-transform" />
<meta http-equiv="Cache-Control" content="no-siteapp" />















  
  
    
  
  <link href="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.min.css" rel="stylesheet" type="text/css" />




  
  
  
  

  
    
    
  

  

  

  

  

  
    
    
    <link href="//fonts.googleapis.com/css?family=Lato:300,300italic,400,400italic,700,700italic&subset=latin,latin-ext" rel="stylesheet" type="text/css">
  






  

<link href="//maxcdn.bootstrapcdn.com/font-awesome/4.6.2/css/font-awesome.min.css" rel="stylesheet" type="text/css" />

<link href="/lh706.github.io/css/main.css?v=5.1.1" rel="stylesheet" type="text/css" />


  <meta name="keywords" content="JavaScript,vue," />








  <link rel="shortcut icon" type="image/x-icon" href="/lh706.github.io/favicon.ico?v=5.1.1" />






<meta name="description" content="一.state — 数据12345678简单使用1.Vue.use(Vuex)2.注入,new vue(&amp;#123;el:...store,...&amp;#125;)3.然后在所有组件中可以通过$store.state.xxx获取,动态数据,通过computed可以实时更新">
<meta name="keywords" content="JavaScript,vue">
<meta property="og:type" content="article">
<meta property="og:title" content="vue-vuex">
<meta property="og:url" content="https://lh706.github.io/lh706.github.io/2017/05/20/vue-vuex/index.html">
<meta property="og:site_name" content="Albert的blog">
<meta property="og:description" content="一.state — 数据12345678简单使用1.Vue.use(Vuex)2.注入,new vue(&amp;#123;el:...store,...&amp;#125;)3.然后在所有组件中可以通过$store.state.xxx获取,动态数据,通过computed可以实时更新">
<meta property="og:updated_time" content="2017-05-20T08:02:07.724Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="vue-vuex">
<meta name="twitter:description" content="一.state — 数据12345678简单使用1.Vue.use(Vuex)2.注入,new vue(&amp;#123;el:...store,...&amp;#125;)3.然后在所有组件中可以通过$store.state.xxx获取,动态数据,通过computed可以实时更新">



<script type="text/javascript" id="hexo.configurations">
  var NexT = window.NexT || {};
  var CONFIG = {
    root: '/lh706.github.io/',
    scheme: 'Mist',
    sidebar: {"position":"right","display":"always","offset":12,"offset_float":0,"b2t":false,"scrollpercent":false},
    fancybox: true,
    motion: true,
    duoshuo: {
      userId: '0',
      author: '博主'
    },
    algolia: {
      applicationID: '',
      apiKey: '',
      indexName: '',
      hits: {"per_page":10},
      labels: {"input_placeholder":"Search for Posts","hits_empty":"We didn't find any results for the search: ${query}","hits_stats":"${hits} results found in ${time} ms"}
    }
  };
</script>



  <link rel="canonical" href="https://lh706.github.io/lh706.github.io/2017/05/20/vue-vuex/"/>





  <title>vue-vuex | Albert的blog</title>
</head>

<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">

  














  
  
    
  

  <div class="container sidebar-position-right page-post-detail ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-wrapper">
  <div class="site-meta ">
    

    <div class="custom-logo-site-title">
      <a href="/lh706.github.io/"  class="brand" rel="start">
        <span class="logo-line-before"><i></i></span>
        <span class="site-title">Albert的blog</span>
        <span class="logo-line-after"><i></i></span>
      </a>
    </div>
      
        <h1 class="site-subtitle" itemprop="description"></h1>
      
  </div>

  <div class="site-nav-toggle">
    <button>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
      <span class="btn-bar"></span>
    </button>
  </div>
</div>

<nav class="site-nav">
  

  
    <ul id="menu" class="menu">
      
        
        <li class="menu-item menu-item-home">
          <a href="/lh706.github.io/" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />
            
            首页
          </a>
        </li>
      
        
        <li class="menu-item menu-item-archives">
          <a href="/lh706.github.io/archives" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />
            
            归档
          </a>
        </li>
      
        
        <li class="menu-item menu-item-tags">
          <a href="/lh706.github.io/tags" rel="section">
            
              <i class="menu-item-icon fa fa-fw fa-tags"></i> <br />
            
            标签
          </a>
        </li>
      

      
    </ul>
  

  
</nav>



 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            

  <div id="posts" class="posts-expand">
    

  

  
  
  

  <article class="post post-type-tags " itemscope itemtype="http://schema.org/Article">
    <link itemprop="mainEntityOfPage" href="https://lh706.github.io/lh706.github.io/lh706.github.io/2017/05/20/vue-vuex/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="name" content="Albert">
      <meta itemprop="description" content="">
      <meta itemprop="image" content="/lh706.github.io/my.jpg">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="Albert的blog">
    </span>

    
      <header class="post-header">

        
        
          <h2 class="post-title" itemprop="name headline">vue-vuex</h2>
        

        <div class="post-meta">
          <span class="post-time">
            
              <span class="post-meta-item-icon">
                <i class="fa fa-calendar-o"></i>
              </span>
              
                <span class="post-meta-item-text">发表于</span>
              
              <time title="创建于" itemprop="dateCreated datePublished" datetime="2017-05-20T15:52:01+08:00">
                2017-05-20
              </time>
            

            

            
          </span>

          

          
            
          

          
          

          

          

          

        </div>
      </header>
    

    <div class="post-body" itemprop="articleBody">

      
      

      
        <h3 id="一-state-—-数据"><a href="#一-state-—-数据" class="headerlink" title="一.state — 数据"></a>一.state — 数据</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div></pre></td><td class="code"><pre><div class="line">简单使用</div><div class="line"><span class="number">1.</span>Vue.use(Vuex)</div><div class="line"><span class="number">2.</span>注入,<span class="keyword">new</span> vue(&#123;</div><div class="line"><span class="attr">el</span>:...</div><div class="line">store,</div><div class="line">...</div><div class="line">&#125;)</div><div class="line"><span class="number">3.</span>然后在所有组件中可以通过$store.state.xxx获取,动态数据,通过computed可以实时更新</div></pre></td></tr></table></figure>
<a id="more"></a>
<h3 id="二-getters-—-获取数据-局部或默认状态-其他getter-根状态"><a href="#二-getters-—-获取数据-局部或默认状态-其他getter-根状态" class="headerlink" title="二.getters — 获取数据 (局部或默认状态,其他getter,根状态)"></a>二.getters — 获取数据 (局部或默认状态,其他getter,根状态)</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">对state中的数据过滤或者计算</div><div class="line">getters:&#123;</div><div class="line">    <span class="attr">todoDone</span>:<span class="function"><span class="params">state</span>=&gt;</span>&#123;</div><div class="line">        <span class="keyword">return</span> state.todos.filter(<span class="function"><span class="params">todo</span>=&gt;</span>todo.done)</div><div class="line">    &#125;,</div><div class="line">    将todos遍历,每条为todo传入匿名函数,返回todo.done为真的那条</div><div class="line">    todoDoneLength:(state,gettrers)&#123;</div><div class="line">        renturn getters.todoDone.length</div><div class="line">    &#125;</div><div class="line">    接受getters作为第二参数,使用getters后的数据</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="三-Mutations-—-数据更新-局部或默认状态-载荷"><a href="#三-Mutations-—-数据更新-局部或默认状态-载荷" class="headerlink" title="三.Mutations — 数据更新 (局部或默认状态,载荷)"></a>三.Mutations — 数据更新 (局部或默认状态,载荷)</h3><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">  <span class="attr">state</span>: &#123;</div><div class="line">    <span class="attr">count</span>: <span class="number">1</span></div><div class="line">  &#125;,</div><div class="line">  <span class="attr">mutations</span>: &#123;</div><div class="line">    increment (state,payload) &#123;</div><div class="line">      <span class="comment">// 变更状态 不能使用异步函数的回调</span></div><div class="line">      state.count=state.count+payload.data</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">然后</div><div class="line">store.commit(<span class="string">"increment"</span>,&#123;</div><div class="line">    <span class="attr">data</span>:<span class="number">10</span></div><div class="line">&#125;)</div><div class="line">或者</div><div class="line">store.commit(&#123;</div><div class="line"><span class="attr">type</span>:<span class="string">"increment"</span>,</div><div class="line"><span class="attr">payload</span>:&#123;&#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">触发事件,更改数据,payload载荷,设置为对象更易读</div></pre></td></tr></table></figure>
<h3 id="四-actions-—-可以异步执行mutations"><a href="#四-actions-—-可以异步执行mutations" class="headerlink" title="四.actions — 可以异步执行mutations"></a>四.actions — 可以异步执行mutations</h3><p>(类store,通过参数解构{state,rootState,commit},内部直接使用,不用带context)<br>(一般在组件中就只调用actions)<br><figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div></pre></td><td class="code"><pre><div class="line">在actions内放置异步函数,然后触发mutation内的事件</div><div class="line">actions:&#123;</div><div class="line">    increment (context)&#123;</div><div class="line">        context.commit(<span class="string">"increment"</span>)</div><div class="line">    &#125;或者</div><div class="line">    increment (&#123;commit&#125;)&#123;</div><div class="line">        commit(<span class="string">"increment"</span>)</div><div class="line">    &#125;</div><div class="line">&#125;</div><div class="line">context是一个与store实例相同的对象</div><div class="line">&#123;commit&#125;是通过参数结构将context中的commit方法当做参数传入</div><div class="line"></div><div class="line">通过store.dispatch(<span class="string">"increment"</span>)</div><div class="line"></div><div class="line">*通过promise等组合actions,即一个actions调用完后调用下一个actions事件</div></pre></td></tr></table></figure></p>
<h3 id="五-modules-—-模块化状态树"><a href="#五-modules-—-模块化状态树" class="headerlink" title="五. modules — 模块化状态树"></a>五. modules — 模块化状态树</h3><blockquote>
<p>类似于vue的模块使用方法</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div></pre></td><td class="code"><pre><div class="line"><span class="keyword">const</span> module_a=&#123;</div><div class="line">    <span class="attr">state</span>:&#123;&#125;,</div><div class="line">    <span class="attr">mutations</span>:&#123;&#125;...</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">var</span> store = <span class="keyword">new</span> Vuex.Store(&#123;</div><div class="line">    <span class="attr">modules</span>:&#123;</div><div class="line">        <span class="attr">a</span>:module_a,</div><div class="line">        <span class="attr">b</span>:module_b</div><div class="line">    &#125;</div><div class="line">&#125;)</div><div class="line"></div><div class="line">然后通过store.state.a.count 访问数据</div><div class="line">mutation 和 getter :  第一个参数是模块的局部状态</div><div class="line">action : context.state 是局部状态，根节点的状态是 context.rootState</div><div class="line">getter : 根状态会作为第三个参数</div><div class="line"></div><div class="line">命名空间---因为mutations等都是全局的,所以通过将变量名设为来设置命名空间</div><div class="line">如:</div><div class="line"><span class="keyword">const</span> TDONE = <span class="string">"TODO/DONE"</span></div><div class="line">然后 type form xxx.js </div><div class="line">[type.TDONE] (state) &#123;</div><div class="line">    ...</div><div class="line">&#125;</div></pre></td></tr></table></figure>
<h3 id="其他"><a href="#其他" class="headerlink" title="其他"></a>其他</h3><p>一.表单处理</p>
<blockquote>
<p>v-model绑定如果是引用类,就会在修改value时,直接修改state<br>解决:</p>
</blockquote>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div></pre></td><td class="code"><pre><div class="line">通过set,get方法</div><div class="line">computed: &#123;</div><div class="line">  <span class="attr">message</span>: &#123;</div><div class="line">    get () &#123;</div><div class="line">      <span class="keyword">return</span> <span class="keyword">this</span>.$store.state.obj.message</div><div class="line">    &#125;,</div><div class="line">    set (value) &#123;</div><div class="line">      <span class="keyword">this</span>.$store.commit(<span class="string">'updateMessage'</span>, value)</div><div class="line">    &#125;</div><div class="line">  &#125;</div><div class="line">&#125;</div></pre></td></tr></table></figure>
      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <div>
      
        

      
    </div>

    <footer class="post-footer">
      
        <div class="post-tags">
          
            <a href="/lh706.github.io/tags/JavaScript/" rel="tag"># JavaScript</a>
          
            <a href="/lh706.github.io/tags/vue/" rel="tag"># vue</a>
          
        </div>
      

      
        
      

      
        <div class="post-nav">
          <div class="post-nav-next post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/git使用/" rel="next" title="git使用">
                <i class="fa fa-chevron-left"></i> git使用
              </a>
            
          </div>

          <span class="post-nav-divider"></span>

          <div class="post-nav-prev post-nav-item">
            
              <a href="/lh706.github.io/2017/05/20/vue-router/" rel="prev" title="vue-router">
                vue-router <i class="fa fa-chevron-right"></i>
              </a>
            
          </div>
        </div>
      

      
      
    </footer>
  </article>



    <div class="post-spread">
      
    </div>
  </div>


          </div>
          


          
  <div class="comments" id="comments">
    
  </div>


        </div>
        
          
  
  <div class="sidebar-toggle">
    <div class="sidebar-toggle-line-wrap">
      <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
      <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
    </div>
  </div>

  <aside id="sidebar" class="sidebar">
    <div class="sidebar-inner">

      

      
        <ul class="sidebar-nav motion-element">
          <li class="sidebar-nav-toc sidebar-nav-active" data-target="post-toc-wrap" >
            文章目录
          </li>
          <li class="sidebar-nav-overview" data-target="site-overview">
            站点概览
          </li>
        </ul>
      

      <section class="site-overview sidebar-panel">
        <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
          <img class="site-author-image" itemprop="image"
               src="/lh706.github.io/my.jpg"
               alt="Albert" />
          <p class="site-author-name" itemprop="name">Albert</p>
           
              <p class="site-description motion-element" itemprop="description"></p>
           
        </div>
        <nav class="site-state motion-element">

          
            <div class="site-state-item site-state-posts">
              <a href="/lh706.github.io/archives">
                <span class="site-state-item-count">20</span>
                <span class="site-state-item-name">日志</span>
              </a>
            </div>
          

          

          
            
            
            <div class="site-state-item site-state-tags">
              <a href="/lh706.github.io/tags/index.html">
                <span class="site-state-item-count">11</span>
                <span class="site-state-item-name">标签</span>
              </a>
            </div>
          

        </nav>

        

        <div class="links-of-author motion-element">
          
            
              <span class="links-of-author-item">
                <a href="https://github.com/lh706" target="_blank" title="GitHub">
                  
                  GitHub
                </a>
              </span>
            
              <span class="links-of-author-item">
                <a href="https://www.zhihu.com/people/liang-da-shu-shu" target="_blank" title="知乎">
                  
                  知乎
                </a>
              </span>
            
          
        </div>

        
        

        
        

        


      </section>

      
      <!--noindex-->
        <section class="post-toc-wrap motion-element sidebar-panel sidebar-panel-active">
          <div class="post-toc">

            
              
            

            
              <div class="post-toc-content"><ol class="nav"><li class="nav-item nav-level-3"><a class="nav-link" href="#一-state-—-数据"><span class="nav-number">1.</span> <span class="nav-text">一.state — 数据</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#二-getters-—-获取数据-局部或默认状态-其他getter-根状态"><span class="nav-number">2.</span> <span class="nav-text">二.getters — 获取数据 (局部或默认状态,其他getter,根状态)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#三-Mutations-—-数据更新-局部或默认状态-载荷"><span class="nav-number">3.</span> <span class="nav-text">三.Mutations — 数据更新 (局部或默认状态,载荷)</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#四-actions-—-可以异步执行mutations"><span class="nav-number">4.</span> <span class="nav-text">四.actions — 可以异步执行mutations</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#五-modules-—-模块化状态树"><span class="nav-number">5.</span> <span class="nav-text">五. modules — 模块化状态树</span></a></li><li class="nav-item nav-level-3"><a class="nav-link" href="#其他"><span class="nav-number">6.</span> <span class="nav-text">其他</span></a></li></ol></div>
            

          </div>
        </section>
      <!--/noindex-->
      

      

    </div>
  </aside>


        
      </div>
    </main>

    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  
  &copy; 
  <span itemprop="copyrightYear">2017</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">Albert</span>
</div>


<div class="powered-by">
  由 <a class="theme-link" href="https://hexo.io">Hexo</a> 强力驱动
</div>

<div class="theme-info">
  主题 -
  <a class="theme-link" href="https://github.com/iissnan/hexo-theme-next">
    NexT.Mist
  </a>
</div>


        

        
      </div>
    </footer>

    
      <div class="back-to-top">
        <i class="fa fa-arrow-up"></i>
        
      </div>
    

  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>









  


  











  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery/2.1.3/jquery.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fastclick/1.0.6/fastclick.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/jquery.lazyload/1.9.3/jquery.lazyload.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/velocity/1.2.3/velocity.ui.min.js"></script>

  
  <script type="text/javascript" src="//cdn.jsdelivr.net/fancybox/2.1.5/jquery.fancybox.pack.js"></script>

  
  <script type="text/javascript" src="/lh706.github.io/lib/canvas-nest/canvas-nest.min.js"></script>


  


  <script type="text/javascript" src="/lh706.github.io/js/src/utils.js?v=5.1.1"></script>

  <script type="text/javascript" src="/lh706.github.io/js/src/motion.js?v=5.1.1"></script>



  
  

  
  <script type="text/javascript" src="/lh706.github.io/js/src/scrollspy.js?v=5.1.1"></script>
<script type="text/javascript" src="/lh706.github.io/js/src/post-details.js?v=5.1.1"></script>



  


  <script type="text/javascript" src="/lh706.github.io/js/src/bootstrap.js?v=5.1.1"></script>



  


  




	





  





  





  






  





  

  

  

  

  

</body>
</html>
